package com.infore.airsupervise.ui.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.infore.airsupervise.R;
import com.infore.airsupervise.utils.DateUtil;
import com.infore.airsupervise.utils.DialogUtil;
import com.infore.airsupervise.utils.ToastUtil;
import com.rey.material.app.DatePickerDialog;
import com.rey.material.app.DialogFragment;
import com.rey.material.widget.RadioButton;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @features: 任务条件检索抽屉布局
 * @author: WuTao
 * @date: 2017/4/1 15:08
 */
public class MissionQueryDrawerFragment extends BaseFragment {
    @BindView(R.id.rb_outer)
    RadioButton rbOuter;
    @BindView(R.id.rb_inner)
    RadioButton rbInner;
    @BindView(R.id.tv_startTime)
    TextView tvStartTime;
    @BindView(R.id.tv_endTime)
    TextView tvEndTime;
    private  ConfigListener configListener;

    //筛选参数
    private int state;
    private String start;
    private String end;


    /**
     * 该界面业务的事件监听
     */
    public interface ConfigListener{
        void onConfigListener(String startTime,String endTime,int state);
    }

    public MissionQueryDrawerFragment() {
    }

    @SuppressLint("ValidFragment")
    public MissionQueryDrawerFragment(ConfigListener configListener) {
        this.configListener = configListener;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.d("MissionQueryDrawer", "setUserVisibleHint: "+isVisibleToUser);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_drawer_mission_config, container, false);
        unbinder = ButterKnife.bind(this, view);
        rbInner.setOnCheckedChangeListener(checkListener);
        rbOuter.setOnCheckedChangeListener(checkListener);
        return view;
    }

    private CompoundButton.OnCheckedChangeListener checkListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(b){
                rbInner.setChecked(rbInner == compoundButton);
                rbOuter.setChecked(rbOuter == compoundButton);
            }
        }
    };

    @OnClick({R.id.tv_startTime, R.id.tv_endTime, R.id.btn_config})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_startTime:
            case R.id.tv_endTime:
                showTimePicker(view.getId());
                break;
            case R.id.btn_config://确认
                if(null != configListener){
                     state = rbInner.isChecked()? 2:1;
                     start = tvStartTime.getText().toString();
                    end = tvEndTime.getText().toString();
                    if(!TextUtils.isEmpty(start) && !TextUtils.isEmpty(end)){
                        if(DateUtil.compareDate(start,end,DateUtil.FORMAT_DATE) <=0){
                            ToastUtil.showShort(getContext(),"结束时间必须大于开始时间");
                            return ;
                        }
                    }
                    configListener.onConfigListener(start,end,state);
                }
                break;
        }
    }

    private  void showTimePicker(final int ResId){

        DialogUtil.getInstance().datePicker(new DialogUtil.OnDialogActionListener() {
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                DatePickerDialog dateDialog = (DatePickerDialog) fragment.getDialog();
                  String date = dateDialog.getFormattedDate(new SimpleDateFormat(DateUtil.FORMAT_DATE));
                if(ResId ==R.id.tv_endTime ){
                    tvEndTime.setText(date);
                }else if(ResId == R.id.tv_startTime){

                    tvStartTime.setText(date);
                }
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {

            }
        }, 0, 0).show(getFragmentManager(), null);
    }
}
